#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
macOS应用打包脚本
使用py2app将PyQt5应用打包为macOS应用程序
"""

import os
import sys
import shutil
import subprocess
from pathlib import Path

def build_macos_app():
    """使用py2app打包macOS应用"""
    print("开始打包macOS应用...")
    
    # 清理之前的构建文件
    dist_dir = Path("dist")
    build_dir = Path("build")
    
    if dist_dir.exists():
        shutil.rmtree(dist_dir)
    if build_dir.exists():
        shutil.rmtree(build_dir)
    
    # 创建图标目录
    icons_dir = Path("icons")
    if not icons_dir.exists():
        icons_dir.mkdir()
    
    # 构建命令
    cmd = [
        sys.executable,
        "setup.py",
        "py2app",
        "--packages=PyQt5",
        "--iconfile=icons/app_icon.icns" if Path("icons/app_icon.icns").exists() else "",
    ]
    
    # 执行打包命令
    try:
        subprocess.run([arg for arg in cmd if arg], check=True)
        print("macOS应用打包成功！应用程序位于: dist/PyQt5桌面应用.app")
    except subprocess.CalledProcessError as e:
        print(f"打包失败: {e}")
        return False
    
    return True

if __name__ == "__main__":
    build_macos_app()